home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Cool Demos, SDKs, & Tools / Demos⁄Tools⁄Offers / Alpha ƒ / Tcl / Menus / ftpMenu.tcl < prev    next >
Text File  |  1999-05-12  |  12KB  |  455 lines

  1. ## -*-Tcl-*- (install)
  2. # ###################################################################
  3. #  Alpha - new Tcl folder configuration
  4. #  FILE: "ftpMenu.tcl"
  5. #                                    created: 20/7/96 {6:02:55 pm} 
  6. #                                last update: 05/12/1999 {22:01:37 PM} 
  7. #  
  8. #  Description: 
  9. # ###################################################################
  10. ##
  11.  
  12. alpha::menu ftpMenu 0.1.2 global "•141" {} {ftpMenu} {} uninstall {this-file} \
  13.   help {[editMark [file join $HOME Help "Alpha Manual"] "Ftp Browser" -r]}
  14.  
  15. hook::register savePostHook ftpPostHook
  16.  
  17. proc ftpMenu {} {}
  18.  
  19. proc ftpPostHook {name} {
  20.     global fetched
  21.     if {[info exists fetched($name)]} {
  22.     set specs $fetched($name)
  23.     # backwards compatibility
  24.     if {[lindex $specs 4] == ""} {
  25.         lappend specs "ftp"
  26.         set fetched($name) $specs
  27.     }
  28.     message "Updating '[file tail $name]' on [car $specs]…"
  29.     if {[string length [lindex $specs 1]]} {
  30.         ftpStore $name [lindex $specs 0] "[cadr $specs]/[file tail $name]" [caddr $specs] [cadddr $specs]
  31.     } else {
  32.         ftpStore $name [lindex $specs 0] "[file tail $name]" [caddr $specs] [cadddr $specs]
  33.     }
  34.     }
  35. }
  36.  
  37. proc rebuildFtpMenu {} {
  38.     global savedMounts recentMounts ftpMenu useCache
  39.     
  40.     Menu -n $ftpMenu -p ftpMenuProc {
  41.     help
  42.     "(-"
  43.     "<S/ibrowse…"
  44.     "<S/i<IbrowseCurrent…"
  45.     "/nbrowseMounts…"
  46.     "(-"
  47.     addMountPoint…
  48.     makePermanent…
  49.     removeMountPoint…
  50.     saveAsAt…
  51.     "(-"
  52.     useCache
  53.     flushCache
  54.     "(-"
  55.     "createFileset"
  56.     "(-"
  57.     }
  58.     markMenuItem -m $ftpMenu "Use Cache" $useCache
  59.     if {[info exists savedMounts]} {
  60.     foreach m [lsort -ignore [array names savedMounts]] {
  61.         addMenuItem -m -l "b " $ftpMenu $m
  62.     }
  63.     }
  64.     if {[info exists recentMounts]} {
  65.     addMenuItem -m $ftpMenu "(-"
  66.     foreach m [lsort -ignore [array names recentMounts]] {
  67.         addMenuItem -m -l "b " $ftpMenu $m
  68.     }
  69.     }
  70. }
  71.  
  72. if {![info exists useCache]} {set useCache 1}
  73.  
  74. app::registerMultiple ftp [list Arch FTCh] [list •141 •315] rebuildFtpMenu
  75.  
  76. proc mountPoints {} {
  77.     global savedMounts recentMounts
  78.     if {[info exists recentMounts]} {
  79.     if {[info exists savedMounts]} {
  80.         set l [concat [array names recentMounts] [array names savedMounts]]
  81.     } else {
  82.         set l [array names recentMounts]]
  83.     }
  84.     } else {
  85.     set l [array names savedMounts]
  86.     }
  87.     return [lsort $l]
  88. }
  89.  
  90.  
  91.  
  92. proc ftpMenuProc {menu item} {
  93.     global modifiedVars modifiedArrVars savedMounts recentMounts PREFS fetched HOME ftpMenu useCache createFtpType
  94.     switch -- $item {
  95.     help                {
  96.         editMark [file join $HOME Help "Alpha Manual"] "Ftp Browser" -r
  97.     }
  98.     browse                {
  99.         eval ftpBrowse [lrange [getLogin {Browse remote machine:} 0] 0 3]
  100.     }
  101.     browseCurrent        { 
  102.         if {[info exists fetched([win::Current])]} {
  103.         eval ftpBrowse $fetched([win::Current]) 
  104.         } else {
  105.         beep; message "'[win::CurrentTail]' not from remote host."
  106.         }
  107.     }
  108.     browseMounts        {
  109.         set l [mountPoints]
  110.         set res [listpick -p "Mount point:" $l]
  111.         if {[info exists recentMounts($res)]} {
  112.         eval ftpBrowse $recentMounts($res)
  113.         } else {
  114.         eval ftpBrowse $savedMounts($res)
  115.         }
  116.     }
  117.     
  118.     addMountPoint        { addMountPoint }
  119.     makePermanent        { makeMountPermanent }
  120.     createFileset        { newFileset ftp }
  121.     removeMountPoint    {
  122.         set pt [listpick -p "Remove which mount point?" [lsort -ignore [array names savedMounts]]]
  123.         unset savedMounts($pt)
  124.         removeArrDef savedMounts $pt
  125.         rebuildFtpMenu
  126.     }
  127.     saveAsAt            {
  128.         global fetched PREFS
  129.         set name [prompt "Name:" [win::CurrentTail]]
  130.         set point [listpick -p "At which mount point?" [mountPoints]]
  131.         if {[info exists recentMounts($point)]} {
  132.         set specs $recentMounts($point)
  133.         } else {
  134.         set specs $savedMounts($point)
  135.         }
  136.         # backwards compatibility
  137.         if {[lindex $specs 4] == ""} {
  138.         lappend specs "ftp"
  139.         }
  140.         set name [file join $PREFS ftptmp $name]
  141.         set fetched($name) $specs
  142.         message "Saving '$name' on [car $specs]…"
  143.         
  144.         if {![file exists $name]} {
  145.         set fid [open $name w]
  146.         close $fid
  147.         }
  148.         saveAs -f "$name"
  149.         
  150.         set num 0
  151.         set pathname [lindex $specs 1]
  152.         for {set i [expr [string length $pathname] - 1]} {$i >= 0} {incr i -1} {
  153.         scan $pathname "%c" char
  154.         incr num $char
  155.         }
  156.         
  157.         set nm [file join $PREFS ftptmp listing.$num]
  158.         catch {rm $nm}
  159.         
  160.         setWinInfo platform $createFtpType
  161.         setWinInfo dirty 1
  162.         save
  163.     }
  164.     
  165.     setDefaults            { 
  166.         global ftpDefaults modifiedVars
  167.         set ftpDefaults [lrange [getLogin "Enter defaults that you wish saved:" 0] 0 3]
  168.         lappend modifiedVars ftpDefaults
  169.     }
  170.     flushCache        { rm [file join $PREFS ftptmp *]; catch {unset recentMounts}; rebuildFtpMenu }
  171.     useCache    { 
  172.         set useCache [expr 1 - $useCache]
  173.         markMenuItem -m $ftpMenu "Use Cache" $useCache
  174.         lappend modifiedVars useCache
  175.     }
  176.     default {
  177.         if {[info exists recentMounts($item)]} {
  178.         eval ftpBrowse $recentMounts($item)
  179.         } else {
  180.         eval ftpBrowse $savedMounts($item)
  181.         }
  182.     }
  183.     }
  184. }
  185.  
  186.  
  187. proc ftpFilesetOpen {menu item} {
  188.     global gfileSets PREFS fetched fileSetsExtra
  189.     
  190.     set ind [lsearch $gfileSets($menu) "$item"]
  191.     if { $ind < 0 } { set ind [lsearch $gfileSets($menu) [file join * $item]] }
  192.  
  193.     if {$ind >= 0} {
  194.     set f [lindex $gfileSets($menu) $ind]
  195.     set lnm [file tail $f]
  196.     regsub -all {:} $f {/} f
  197.     set nm [file join $PREFS ftptmp $lnm]
  198.     set specs $fileSetsExtra($menu)
  199.     # backwards compatibility
  200.     if {[lindex $specs 4] == ""} {
  201.         lappend specs "ftp"
  202.         set fileSetsExtra($menu) $specs
  203.     }
  204.     if {![file exists $nm]} {
  205.         ftpFetch $nm [car $specs] $f [caddr $specs] [cadddr $specs]
  206.     }
  207.     edit -w $nm
  208.     set fetched($nm) $specs
  209.     }
  210. }
  211.  
  212.  
  213. proc ftpCreateFileset {} {
  214.     global gfileSets gfileSetsType PREFS fileSetsExtra
  215.     
  216.     set specs [getLogin]
  217.     set name [car $specs]
  218.     set host [cadr $specs]
  219.     set path [caddr $specs]
  220.     set user [cadddr $specs]
  221.     set password [caddddr $specs]
  222.     set pattern "^[prompt {Name pattern?} {.*.[ch]}]$"
  223.     set path [string trimright $path {/}]
  224.     
  225.     set fileSetsExtra($name) [list $host $path $user $password "ftp"]
  226.     
  227.     if { ![file exists [file join $PREFS ftptmp]] } {
  228.     file mkdir [file join $PREFS ftptmp]
  229.     }
  230.     set nm [file join $PREFS ftptmp listing.$path]
  231.     ftpList $nm $host $path $user $password
  232.     set files {}
  233.     foreach f [processListing $nm] {
  234.     if {![string match {*/} $f] && [regexp -- $pattern $f]} {
  235.         lappend files "$path/$f"
  236.     }
  237.     }
  238.     regsub -all {/} $files {:} files
  239.     
  240.     global gfileSets gfileSetsType
  241.     set gfileSets($name) [lsort -command sortByTail $files]
  242.     set gfileSetsType($name) ftp
  243.     if {[askyesno "Save project fileset?"] == "yes"} {
  244.     addArrDef gfileSetsType $name ftp
  245.     addArrDef gfileSets $name  $gfileSets($name)
  246.     addArrDef fileSetsExtra $name $fileSetsExtra($name)
  247.     }
  248.     return $name
  249. }
  250.  
  251.  
  252. proc processListing {path} {
  253.     set fd [open $path "r"]
  254.     set lines [split [read $fd] "\n"]
  255.     close $fd
  256.     set files {}
  257.     if {[llength $lines]} {
  258.     if {[string length [lindex $lines 0]] <= 10} {
  259.         set lines [cdr [lreplace $lines end end]]
  260.     } else {
  261.         set lines [lreplace $lines end end]
  262.     }
  263.     foreach f $lines {
  264.         set nm {}
  265.         regexp {[A-Z][a-z]+ [0-9, ]+ [0-9,:]+ (.*)$} $f dummy nm
  266.         if {[string length $nm]} {
  267.         if {[string match "d*" $f]} {
  268.             if {![string match "." $nm] && ![string match ".." $nm]} {
  269.             lappend files "$nm/"
  270.             }
  271.         } else {
  272.             lappend files $nm
  273.         }
  274.         }
  275.     }
  276.     } else {
  277.     error "empty list"
  278.     }
  279.     return $files
  280. }
  281.  
  282. proc getLogin {{prompt {All but 'password' are required:}} {nm 1}} {
  283.     global ftpDefaults
  284.     if {[info exists ftpDefaults]} {
  285.     set defs $ftpDefaults
  286.     } else {
  287.     set defs {"" "" "" ""}
  288.     }
  289.     set left 10
  290.     set right 100
  291.     set top 10
  292.     set bottom 30
  293.     set eleft [expr $left + 100]
  294.     set eright 370
  295.     set incr 30
  296.     
  297.     set height 198
  298.     
  299.     if {$nm} {incr height $incr}
  300.     set l "dialog -w 400 -h $height -t [list $prompt] $left $top 400 $bottom"
  301.     
  302.     if {$nm} {
  303.     incr top $incr
  304.     incr bottom $incr
  305.     lappend l -t {Name:} $left $top $right $bottom
  306.     lappend l -e {} $eleft $top $eright $bottom
  307.     }
  308.     
  309.     incr top $incr
  310.     incr bottom $incr
  311.     lappend l -t {Host:} $left $top $right $bottom
  312.     lappend l -e [car $defs] $eleft $top $eright $bottom
  313.     
  314.     incr top $incr
  315.     incr bottom $incr
  316.     lappend l -t {Path:} $left $top $right $bottom
  317.     lappend l -e [cadr $defs] $eleft $top $eright $bottom
  318.     
  319.     incr top $incr
  320.     incr bottom $incr
  321.     lappend l -t {UserID:} $left $top $right $bottom
  322.     lappend l -e [caddr $defs] $eleft $top $eright $bottom
  323.     
  324.     incr top $incr
  325.     incr bottom $incr
  326.     lappend l -t {Password:} $left $top $right $bottom
  327.     lappend l -e [cadddr $defs] $eleft [expr $top + 6] $eright [expr $bottom - 12]
  328.     
  329.     incr top [expr $incr + 10]
  330.     incr bottom [expr $incr + 10]
  331.     lappend l -b "OK" $left $top $right [expr $top + 20]
  332.     lappend l -b "Cancel" [expr $left + 200] $top [expr $right + 200] [expr $top + 20]
  333.     
  334.     set res [eval "$l"]
  335.     if {[lindex $res end]} {error "Cancel"}
  336.     return $res
  337. }
  338.  
  339.  
  340. proc addMountPoint {} {
  341.     global savedMounts modifiedArrVars
  342.     
  343.     set res [getLogin]
  344.     if {[lindex $res 5]} {
  345.     set savedMounts([car $res]) [concat [lrange $res 1 4] "ftp"]
  346.     lappend modifiedArrVars savedMounts
  347.     rebuildFtpMenu
  348.     }
  349. }
  350.  
  351.  
  352. proc makeMountPermanent {} {
  353.     global recentMounts savedMounts modifiedArrVars
  354.     if {![info exists recentMounts]} {
  355.     alertnote "You have no temporary mounts."
  356.     return
  357.     }
  358.     set res [listpick -p "Make which temporary mount point permanent?" [lsort [array names recentMounts]]]
  359.     set name [prompt "Name?" $res]
  360.     set savedMounts($name) $recentMounts($res)
  361.     unset recentMounts($res)
  362.     lappend modifiedArrVars savedMounts
  363.     rebuildFtpMenu
  364. }
  365.  
  366.  
  367. proc ftpPromptBrowse {} {
  368.     eval ftpBrowse [lrange [getLogin {Browse remote machine:} 0] 0 3]
  369. }
  370.  
  371. proc ftpBrowse {host dir user password {type "ftp"} {fname {}}} {
  372.     global PREFS fetched lastFtpDir recentMounts savedMounts useCache
  373.     
  374.     watchCursor
  375.     if {![string length $password]} {
  376.     set password [dialog::password "Password for ${host}:"]
  377.     }
  378.     
  379.     if {![file exists [file join $PREFS ftptmp]]} {
  380.     file mkdir [file join $PREFS ftptmp]
  381.     }
  382.     if {$dir == {-}} {
  383.     if {![info exists lastFtpDir] || ![string length $lastFtpDir]} {set lastFtpDir ""}
  384.     set dir [prompt "'$host' dir:" $lastFtpDir]
  385.     }
  386.     set dir [string trimright $dir {/}]
  387.     set lastFtpDir $dir
  388.     
  389.     set num 0
  390.     for {set i [expr [string length $dir] - 1]} {$i >= 0} {incr i -1} {
  391.     scan [string index $dir $i] "%c" char
  392.     incr num $char
  393.     }
  394.     
  395.     set nm [file join $PREFS ftptmp listing.$num]
  396.     
  397.     if {!$useCache || ![file exists $nm]} {
  398.     ftpList $nm $host $dir $user $password
  399.     }
  400.     if {[catch {processListing $nm} listing]} {
  401.     alertnote "Error fetching directory '$dir'"
  402.     error "Error fetching directory '$dir'"
  403.     }
  404.     set files [concat {..} $listing]
  405.     if {$fname != ""} {
  406.     set file [listpick -L $fname -p "$dir/" $files]
  407.     } else {
  408.     set file [listpick -p "$dir/" $files]
  409.     }
  410.     
  411.     if {$file == {..}} {
  412.     if {[regexp {(.+)/[^/]+} $dir dummy sub]} {
  413.         return [ftpBrowse $host $sub $user $password]
  414.     } else {
  415.         return [ftpBrowse $host "" $user $password]
  416.     }
  417.     }
  418.     
  419.     if {[string match {*/} $file]} {
  420.     if {[string length $dir]} {
  421.         return [ftpBrowse $host [string trimright "$dir/$file" {/}] $user $password]
  422.     } else {
  423.         return [ftpBrowse $host [string trimright "$file" {/}] $user $password]
  424.     }
  425.     }
  426.     
  427.     set entry [list $host $dir $user $password $type]
  428.     set new 1
  429.     foreach name [array names savedMounts] {
  430.     if {([car $savedMounts($name)] == [car $entry]) && ([cadr $savedMounts($name)] == [cadr $entry])} {
  431.         set new 0
  432.         break;
  433.     }
  434.     }
  435.     if {$new} {
  436.     set recentMounts($dir) $entry
  437.     rebuildFtpMenu
  438.     }
  439.     
  440.     set nm [file join $PREFS ftptmp $file]
  441.     if {!$useCache || ![file exists $nm]} {
  442.     if {[string length $dir]} {
  443.         ftpFetch $nm $host "$dir/$file" $user $password
  444.     } else {
  445.         ftpFetch $nm $host "$file" $user $password
  446.     }
  447.     }
  448.     edit -w $nm
  449.     set fetched($nm) [list $host $dir $user $password "ftp"]
  450. }
  451.  
  452.  
  453.